package com.ms.p2p.controller.admin;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.utils.MsDateUtils;
import com.ms.common.utils.excel.ExcelData;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.provider.DictionaryProvider;
import com.ms.p2p.provider.YeahpayLogProvider;
import com.ms.p2p.utils.JsonDataGridHelper;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


/**
 * 活期宝
 */
@Controller
@Transactional
@RequestMapping({ "/admin/yuebaoLog" })
public class AdminWalletController extends AdminBaseController {
	@Reference
	private YeahpayLogProvider yeahpayLogProvider;

	@Reference
	private DictionaryProvider dictionaryProvider;
	@RequestMapping({ "/list" })
	public String list(HttpServletRequest request, HttpSession session) {
		return "admin/account/yuebao/log/list";
	}
	@RequestMapping(value = {"/queryData"}, produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public PageBean queryData(HttpServletRequest request, HttpSession session

			, @RequestParam(value = "userId",required = false) String userId
			, @RequestParam(value = "userName",required = false) String userName
			, @RequestParam(value = "logType",required = false) String logType
			, @RequestParam(value = "begin_date",required = false) String begin_date
			, @RequestParam(value = "end_date",required = false) String end_date,
							  PageBean page
	) {
		page.setList(yeahpayLogProvider.getPager(
				userId, userName, logType, begin_date, end_date, page));
		page.setTotalCount(yeahpayLogProvider.getCount(userId, userName, logType,
				begin_date, end_date));
		return JsonDataGridHelper.createJSONData(page);
	}
	@RequestMapping({ "/exportYuEbaoLogRecords" })
	public String exportYuEbaoLogRecords(HttpServletRequest request, Model model
			, @RequestParam(value = "logType",required = false) String logType
			, @RequestParam(value = "begin_date",required = false) String begin_date
			, @RequestParam(value = "end_date",required = false) String end_date
			, @RequestParam(value = "userId",required = false) String userId
			, @RequestParam(value = "userName",required = false) String userName

	) {
		PageBean page = new PageBean();
		page.setCurrentPage(1);
		page.setPageSize(null);
		List<Map<String, Object>> yuEbaoLogs = yeahpayLogProvider.getPager(
				userId, userName, logType, begin_date, end_date, page);

		for (Map<String, Object> map : yuEbaoLogs) {
			Object type = map.get("type");
			if (type != null) {
				map.put("type", dictionaryProvider.getDictValue("yuebao_log_type",type+""));
			}
			Object money = map.get("money");
			if (money != null) {
				map.put("money",  money.toString());
			}
			Object total = map.get("total");
			if (total != null) {
				map.put("total",  total.toString());
			}
			Object yesterday_income = map.get("yesterday_income");
			if (yesterday_income != null) {
				map.put("yesterday_income",  yesterday_income.toString());
			}
			Object total_income = map.get("total_income");
			if (total_income != null) {
				map.put("total_income",  total_income.toString());
			}
			String addtime = (String) map.get("addtime");
			if (addtime != null) {
				map.put("addtime", MsDateUtils.TimeStamp2DateTime(addtime));
			}
		}
		ExcelData excelData = new ExcelData();
		excelData.setHeads(new String[] { "用户ID", "用户名称", "记录编号", "记录类型",
				"操作金额", "总额", "昨日收益", "累计收益", "备注", "时间" });
		excelData.setFieldNames(new String[] { "user_id", "u_user_name", "id",
				"type", "money", "total", "yesterday_income", "total_income",
				"remark", "addtime" });
		excelData.setDataList(yuEbaoLogs);
		excelData.setModelClass(Map.class);
		model.addAttribute("excelData", excelData);
		model.addAttribute("fileName", "活期宝记录");

		return "excel";
	}

	@Override
	public boolean getBinderFlag() {
		return true;
	}
}
